
<template>
  <!-- 放置表单 -->
  <el-form
    ref="userForm"
    :rules="rules"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input
        v-model="userInfo.password"
        style="width: 300px"
        type="password"
        disabled
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="upLoadInfo">更新</el-button>
      <el-button @click="$router.back()">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserDetailById, saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        password: ''
      },
      rules: {
        userName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.loadUserInfo()
  },
  methods: {
    async loadUserInfo() {
      const res = await getUserDetailById(this.$route.query.id)
      //   console.log(res)
      this.userInfo = res.data
    },
    // 点击更新按钮
    upLoadInfo() {
      this.$refs.userForm.validate(async(valid) => {
        if (valid) {
          await saveUserDetailById(this.userInfo)
          this.$message.success('更新成功')
        }
      // 使用最新修改数据覆盖源数据 作为接口提交数据
      })
    }
  }
}
</script>
